<?php
# ***** BEGIN LICENSE BLOCK *****
# This file is part of "myWiWall".
# Copyright (c) 2007-2008 CRP Henri Tudor and contributors.
# All rights reserved.
#
# "myWiWall" is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# 
# "myWiWall" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with "myWiWall"; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# ***** END LICENSE BLOCK *****


/**
 * The AdminCategories module unites the actions relevant to the administration
 * of the Widget Categories in the portal.
 * 
 * @package mywiwall.actions
 * @author J�r�me Bogaerts <jerome.bogaerts@gmail.com>
 */
class AdminCategories
{
	/**
	 * This action provides the user features to manage widget categories in the portal. 
	 */
	public function index()
	{
		// Session message handling for category addition/edition/deletion.
		if (isset($_SESSION['message']))
		{
			$message = $_SESSION['message'];
			$isError = $_SESSION['isError'];
			
			unset($_SESSION['message']);
			unset($_SESSION['isError']);
		}
	
		// Security check.
		if (!Auth::isAuth() && (Auth::isAdmin() || Auth::isGod()))
			DefaultFC::redirection('users/index?ref=admin');	
			
		// Determine if the view must allow the user to manage widgets.
		$widgetManagement = false;
		if (Auth::isAdmin() || Auth::isGod())
			$widgetManagement = true;	
		
		$categories = Categories::retrieveCategories('raw');
		$partial = 'categories';
		
		require (DefaultFC::getView('admin.tpl'));
	}
	
	/**
	 * This action is triggered through a POST HTTP Request in order
	 * to add a new widget category in the system.
	 * 
	 * @param string $category The name of the category the user needs to create.
	 */
	public function add($category)
	{
		// Security check.
		if (!Auth::isAuth() && (Auth::isAdmin() || Auth::isGod()))
			DefaultFC::redirection('users/index?ref=admin');
			
		try
		{
			Categories::add($category);
			
			// Error information for the view.
			$_SESSION['isError'] = false;
			$_SESSION['message'] = sprintf(__("The category '%s' has been successfuly created."), $category);
			
			DefaultFC::redirection('adminCategories/index');
		}
		catch(CategoryException $e)
		{
			$_SESSION['isError'] = true;
			
			if ($e->getCode() == CategoryException::ALREADY_EXISTS)
				$_SESSION['message'] = sprintf(__("The category '%s' already exists."), $category);
			else
				$_SESSION['message'] = __("Please provide a category name.");
			
			DefaultFC::redirection('adminCategories/index');
		}
	}
	
	/**
	 * This action is triggered through a POST HTTP Request in order to modifiy
	 * the name of an existing category.
	 * 
	 * @param integer $categoryId The numeric identifier of the category the user wants to modify the name.
	 * @param string $catregoryName The new name of the category the user wants to modify the name.
	 */
	public function edit($categoryId, $categoryName)
	{
		// Security check.
		if (!Auth::isAuth() && (Auth::isAdmin() || Auth::isGod()))
			DefaultFC::redirection('users/index?ref=admin');
			
		try
		{
			Categories::modify($categoryId, $categoryName);
			
			// Ok no exception thrown !
			$_SESSION['isError'] = false;
			$_SESSION['message'] = sprintf(__("The category '%s' has been successfuly updated."), $categoryName);
			
			DefaultFC::redirection('adminCategories/index');
		}
		catch (CategoryException $e)
		{
			// An error occured. Normally, the category name already exists.
			$_SESSION['isError'] = true;
			
			if ($e->getCode() == CategoryException::ALREADY_EXISTS)
				$_SESSION['message'] = sprintf(__("The category '%s' already exists."), $categoryName);
			else
				$_SESSION['message'] = __("Please provide a category name.");
			
			DefaultFC::redirection('adminCategories/index');
		}
	}
	
	/**
	 * The delete action is triggered through a POST HTTP request when a user wants to delete
	 * a specific category.
	 */
	public function delete($categoryId)
	{
		Categories::delete($categoryId);
		
		$_SESSION['isError'] = false;
		$_SESSION['message'] = sprintf(__("The category has been successfuly deleted."));
		
		DefaultFC::redirection('adminCategories/index');
	}
}
?>